﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DC2010_Editor.Properties;
using DC2010.Tools;
using DC2010.Objects;

namespace DC2010_Editor.Formulars
{
    public partial class ItemsPlacement : Form
    {
        private DungeonPlacement data { get; set; }
        private List<DungeonPlacement> _fiList { get; set; }

        public ItemsPlacement(List<DungeonPlacement> fiList)
        {
            _fiList = fiList;

            if (_fiList == null) _fiList = new List<DungeonPlacement>();

            InitializeComponent();

            ImageListBoxItem itm1 = new ImageListBoxItem("CENTER\nCenter", Resources._5C, "C");
            ImageListBoxItem itm2 = new ImageListBoxItem("North wall\nNorth part of tile or wall", Resources._5N, "N");
            ImageListBoxItem itm3 = new ImageListBoxItem("West wall\nWest part of tile or wall", Resources._5W, "W");
            ImageListBoxItem itm4 = new ImageListBoxItem("South wall\nSouth part of tile or wall", Resources._5S, "S");
            ImageListBoxItem itm5 = new ImageListBoxItem("East wall\nEast part of tile or wall", Resources._5E, "E");

            ImageListBoxItem itm22 = new ImageListBoxItem("North-west corner\nNorth-west corner of tile", Resources._51, "NW");
            ImageListBoxItem itm33 = new ImageListBoxItem("North-east corner\nNorth-east corner of tile", Resources._52, "NE");
            ImageListBoxItem itm44 = new ImageListBoxItem("South-east corner\nSouth-east corner of tile", Resources._53, "SE");
            ImageListBoxItem itm55 = new ImageListBoxItem("South-west ocrner\nSouth-west corner of tile", Resources._54, "SW");

            imageListBox1.Items.Add(itm1);
            imageListBox1.Items.Add(itm2);
            imageListBox1.Items.Add(itm3);
            imageListBox1.Items.Add(itm4);
            imageListBox1.Items.Add(itm5);

            imageListBox1.Items.Add(itm22);
            imageListBox1.Items.Add(itm33);
            imageListBox1.Items.Add(itm44);
            imageListBox1.Items.Add(itm55);
        }

        private void ItemsPlacement_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private new void Refresh()
        {
            if (_fiList != null)
            {
                imageListBox2.Items.Clear();

                foreach (DungeonPlacement dp in _fiList)
                {
                    Image i = Resources._5C; //default

                    if (dp.ObjectPosition == ObjectPosition.Center) i = Resources._5C;
                    if (dp.ObjectPosition == ObjectPosition.East) i = Resources._5E;
                    if (dp.ObjectPosition == ObjectPosition.West) i = Resources._5W;
                    if (dp.ObjectPosition == ObjectPosition.South) i = Resources._5S;
                    if (dp.ObjectPosition == ObjectPosition.North) i = Resources._5N;


                    if (dp.ObjectPosition == ObjectPosition.NorthWest) i = Resources._51;
                    if (dp.ObjectPosition == ObjectPosition.NorthEast) i = Resources._52;
                    if (dp.ObjectPosition == ObjectPosition.SouthEast) i = Resources._53;
                    if (dp.ObjectPosition == ObjectPosition.SouthWest) i = Resources._54;

                    ImageListBoxItem itm = new ImageListBoxItem(dp.ObjectId, i, "XXXX");
                    imageListBox2.Items.Add(itm);
                }
            }
        }

        public void SetControls()
        {
            ObjectIdTextBox.Text = data.ObjectId;
        }

        public void GetFromControls()
        {
            data.ObjectId = ObjectIdTextBox.Text;

            string sLocation = "C";

            if (imageListBox1.SelectedItem != null)
            {
                sLocation = ((ImageListBoxItem)imageListBox1.SelectedItem).Tag.ToString();
            }

            switch (sLocation)
            {
                case "C": { data.ObjectPosition = ObjectPosition.Center; break; }
                case "N": { data.ObjectPosition = ObjectPosition.North; break; }
                case "S": { data.ObjectPosition = ObjectPosition.South; break; }
                case "W": { data.ObjectPosition = ObjectPosition.West; break; }
                case "E": { data.ObjectPosition = ObjectPosition.East; break; }
                case "NE": { data.ObjectPosition = ObjectPosition.NorthEast; break; }
                case "NW": { data.ObjectPosition = ObjectPosition.NorthWest; break; }
                case "SE": { data.ObjectPosition = ObjectPosition.SouthEast; break; }
                case "SW": { data.ObjectPosition = ObjectPosition.SouthWest; break; }
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            data = new DungeonPlacement();

            GetFromControls();
            //we filled data object.

            if ((_fiList!=null)&&(!_fiList.Contains(data)))
                _fiList.Add(data);

            Refresh();
        }

        private void ModifyButton_Click(object sender, EventArgs e)
        {
            GetFromControls();
            //we filled data object.

            if ((_fiList!=null)&&(!_fiList.Contains(data)))
                _fiList.Add(data);

            Refresh();
        }

        private void imageListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (imageListBox2.SelectedItem != null)
            {
                int index = imageListBox2.SelectedIndex;

                if (_fiList.Count>=index)
                {
                    DungeonPlacement dp = _fiList[index];
                    data = dp;
                    ObjectIdTextBox.Text = dp.ObjectId;
                }
            }
        }
    }
}
